package com.leetcode.partition1;

import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2021/8/28 10:54
 */
public class LC55跳跃游戏 {

    public static boolean canJump(int[] nums) {
        return dynamicProgramming(nums);
    }

    public static void main(String[] args) {
        int[] nums = {3, 2, 1, 0, 4};
//        int[] nums = {2, 3, 1, 1, 4};
        System.out.println(canJump(nums));
    }

    private static boolean dynamicProgramming(int[] nums) {
        boolean[] dp = new boolean[nums.length];
        dp[0] = true;       //起点必定是能够到达的
        for (int i = 0; i < nums.length; i++) {
            //j <= i + nums[i]保证能够一步到达
            for (int j = i; j < nums.length && j <= i + nums[i]; j++) {
                dp[j] = dp[i];      //如果dp[i]都不能到达，就更不可能通过i到达j
            }
        }
        System.out.println(Arrays.toString(dp));
        return dp[dp.length - 1];
    }

    private static boolean greedy(int[] nums) {
        int cover = nums[0];
        if (nums.length == 1) return true;
        for (int i = 1; i <= cover; i++) {
            System.out.println(cover);
            cover = Math.max(cover, i + nums[i]);       //补充覆盖范围
            if (cover >= nums.length - 1) return true;
        }
        return false;
    }

    private static boolean greedy2(int[] nums) {
        if (nums.length == 0) return true;
        int remainPower = nums[0], i = 1;
        for (; remainPower != 0 && i < nums.length; i++) {          //能量没消耗完就可以继续移动
            remainPower--;
            if (remainPower < nums[i]) {                //剩余能量没有补充能量大就进行能量补充
                remainPower = nums[i];
            }
        }
        return i == nums.length;
    }
}
